import { BasicDrawer, useDrawerInner } from "@/components/Drawer";
import { BasicForm, FormSchema, useForm } from "@/components/Form";
import { productApi } from "@/api/erp/drug/drug";
import { drugCategoryApi } from "@/api/erp/drug/category";
import { message } from "antd";
import { useTranslation } from "react-i18next";
import { useState, useMemo } from "react";
import { drugUnitApi } from "@/api/erp/drug/unit";
let reload: Function;
let productId: any;
const index = (props: { register: Fn }) => {
	const [isUpdated, setIsUpdated] = useState(false);
	const { t } = useTranslation();
	const en_vi = "drugStorage";
	const formSchema: FormSchema[] = [
		{
			label: t(`${en_vi}.inventoryManagement.drugInventory.drugName`),
			field: "drugName",
			component: "Input"
		},
		{
			label: t("felix_ui.barCode"),
			field: "barCode",
			component: "Input"
		},
		{
			label: t(`${en_vi}.drugManagement.drugInfo.drugCats`),
			field: "drugCats",
			component: "ApiSelect",
			componentProps: {
				api: drugCategoryApi.getdrugCategorySimpleList,
				labelField: "cateName",
				valueField: "cateId"
			}
		},
		{
			label: t("felix_ui.unitName"),
			field: "unitId",
			component: "ApiSelect",
			componentProps: {
				api: drugUnitApi.getdrugUnitSimpleList,
				labelField: "unitName",
				valueField: "unitId",
				immediate: false
			}
		},
		{
			label: t("felix_ui.status"),
			field: "status",
			component: "RadioGroup",
			componentProps: {
				options: [
					{ label: t(`${en_vi}.inventoryManagement.warehouseInformation.open`), value: "0" },
					{ label: t(`${en_vi}.inventoryManagement.warehouseInformation.close`), value: "1" }
				]
			}
		},
		{
			label: t("felix_ui.drugSpec"),
			field: "drugSpec",
			component: "Input"
		},
		{
			label: t("felix_ui.storageCond"),
			field: "storageCond",
			component: "Input"
		},
		{
			label: t("felix_ui.drugAds"),
			field: "drugAds",
			component: "Input"
		},
		{
			label: t("felix_ui.dosePerTime"),
			field: "dosePerTime",
			component: "Input"
		},
		{
			label: t("felix_ui.activeIngre"),
			field: "activeIngre",
			component: "Input"
		},
		{
			label: t("felix_ui.sideEffects"),
			field: "sideEffects",
			component: "Input"
		},
		{
			label: t("felix_ui.timesPerDay"),
			field: "timesPerDay",
			component: "Input"
		},
		{
			label: t("felix_ui.expiryDays"),
			field: "expiryDays",
			component: "InputNumber"
		},
		{
			label: t("felix_ui.weight"),
			field: "weight",
			component: "InputNumber"
		},
		{
			label: t("felix_ui.purPrice"),
			field: "purPrice",
			component: "InputNumber"
		},
		{
			label: t("felix_ui.salePrice"),
			field: "salePrice",
			component: "InputNumber"
		},
		{
			label: t("felix_ui.minPrice"),
			field: "minPrice",
			component: "InputNumber"
		},
		{
			label: t("felix_ui.remark"),
			field: "remark",
			component: "InputTextArea"
		}
	];
	const getTitle = useMemo(() => {
		return isUpdated ? t("felix_ui.edit") : t("felix_ui.add");
	}, [isUpdated]);
	const [register, { validateFields, setFieldsValue, resetFields }] = useForm({
		schemas: formSchema,
		labelWidth: 120,
		baseColProps: {
			span: 24
		}
	});
	const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(props.register, data => {
		reload = data.reload;
		productId = data.record?.productId;
		resetFields();
		setDrawerProps({ confirmLoading: true });
		setIsUpdated(!!data?.isUpdated);
		setFieldsValue({
			...data.record
		});
		setDrawerProps({ confirmLoading: false });
	});
	const handleSubmit = async () => {
		const formValue = await validateFields();
		formValue.drugs = { ...formValue, drugId: productId };
		formValue.bizType = "1";
		formValue.productName = formValue.drugName;
		let res;
		if (isUpdated) {
			formValue.productId = productId;
			res = await productApi.updateproduct(formValue);
		} else {
			res = await productApi.createproduct(formValue);
		}
		if (!res) {
			message.error(t("viewComponents.patientInformation.additionFailed"));
			return;
		}
		message.success(t("viewComponents.patientInformation.successfullyAdded"));
		closeDrawer();
		reload();
	};
	return (
		<BasicDrawer title={getTitle} showFooter={true} {...props} register={registerDrawer} ok={handleSubmit}>
			<BasicForm register={register} labelWrap={true}></BasicForm>
		</BasicDrawer>
	);
};
export default index;
